package com.crr.servlet;

import com.alibaba.fastjson.JSON;
import com.crr.mapper.AdminMapper;
import com.crr.mapper.EmpMapper;
import com.crr.pojo.Admin;
import com.crr.pojo.Dept;
import com.crr.pojo.Emp;
import com.crr.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/show")
public class ShowServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
        EmpMapper em = sqlSession.getMapper(EmpMapper.class);
        int sal= (int) req.getSession().getAttribute("sal");
        int deptno= (int) req.getSession().getAttribute("deptno");

        Emp emp = new Emp();
        emp.setSal(sal);
        Dept dept = new Dept();
        dept.setDeptno(deptno);
        emp.setDept(dept);
        resp.setContentType("application/json;charset=UTF-8");
        List<Emp> list = em.getEmpBySalAndDept(emp);
                PrintWriter out = resp.getWriter();
                out.print(JSON.toJSONString(list));
                out.flush();
/*
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        req.setAttribute("list",list);
        req.getRequestDispatcher("show.jsp").forward(req,resp);*/

    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
